package com.campus.dao.provider;

import com.campus.pojo.ProUnitInfo;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

public class UnitInfoDynSqlProvider {   //分页查询
    public String selectByPage (Map<String,Object> params){
        String sql = new SQL(){
            {
                SELECT("a.name as name,b.name as unitType,a.outside_code as outside_code");
                FROM("pro_unitinfo as a,pro_paraminfo as b");
                WHERE("a.unitTypeId = b.id");
                WHERE("a.delState = 1");
                if (params.get("areaId") != null){
                    WHERE("a.areaId = #{areaId}");
                }
                if (params.get("nameOrCode") != null  && !((String) (params.get("nameOrCode"))).equals("")){  //名称或单位编号
                    WHERE("(a.name like concat(concat('%',#{nameOrCode}),'%') or " +
                            "a.outside_code like concat(concat('%',#{nameOrCode}),'%'))");
                }
                if (params.get("unitTypeId") != null && !((String) (params.get("unitTypeId"))).equals("")){  //类型
                    WHERE("a.unitTypeId = #{unitTypeId}");
                }
              /*  if (params.get("unitGradeId") != null){ // 单位级别？？
                    WHERE("a.unitGradeId = #{unitGradeId}");
                }*/
            }
        }.toString();
        //System.out.println(sql);//调试
        sql += " limit #{pager.firstLimitParam},#{pager.perPageRows}";
        return sql;
    }

    public String count(Map<String,Object> params){ //满足条件的记录个数
        String sql = new SQL(){
            {
                SELECT("count(*)");
                FROM("pro_unitinfo as a,pro_paraminfo as b");
                WHERE("a.unitTypeId = b.id");
                WHERE("a.delState = 1");
                if (params.get("areaId") != null){
                    WHERE("a.areaId = #{areaId}");
                }
                if (params.get("nameOrCode") != null && !((String) (params.get("nameOrCode"))).equals("")){  //名称或单位编号
                    WHERE("a.name like concat(concat('%',#{nameOrCode}),'%') or " +
                            "a.outside_code like concat(concat('%',#{nameOrCode}),'%')");
                }
                if (params.get("unitTypeId") != null  && !((String) (params.get("unitTypeId"))).equals("")){  //类型
                    WHERE("a.unitTypeId = #{unitTypeId}");
                }
                /*if (params.get("unitGradeId") != null){ // 单位级别？？
                    WHERE("a.unitGradeId = #{unitGradeId}");
                }*/
            }
        }.toString();
        //System.out.println(sql);//调试
        return sql;
    }

    public String getInfo(Map<String, Object> params) {
        String areaId = (String) params.get("areaId");
        String nameOrCode = (String) params.get("nameOrCode");
        String unitTypeId = (String) params.get("unitTypeId");
        String unitGradeId = (String) params.get("unitGradeId");
        String sql = "select * from pro_unitinfo where delState=1";
        sql += " and areaId='" + areaId + "'";

        if (nameOrCode != null && !nameOrCode.isEmpty()) {
            sql += " and (name like '%" + nameOrCode + "%' or outside_code like '%" + nameOrCode + "%')";
        }
        if (unitTypeId != null && !unitTypeId.isEmpty()) {
            sql += " and unitTypeId='" + unitTypeId + "'";
        }
        if (unitGradeId != null && !unitGradeId.isEmpty()) {
            sql += " and unitGradeId='" + unitGradeId + "'";
        }
        sql += " order by outside_code desc";
        //System.out.println(sql);//调试
        return sql;

    }
}
